package com.zues.thread.lock.synchronize;

import lombok.extern.slf4j.Slf4j;

/**
 * 演示 synchronized
 * @author zues
 * @date 3/1/2025 10:20 PM
 */
@Slf4j(topic = "c.Demo1")
public class Demo1 {


    static int counter = 0;

    //static修饰，则元素是属于类本身的，不属于对象  ，与类一起加载一次，只有一个
    static final Object room = new Object();

    public static void main(String[] args) throws InterruptedException {

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                synchronized (room){
                    counter++;
                    log.info("counter: {} ", counter);
                }
            }

        }, "t1");


        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                synchronized (room){
                    counter--;
                    log.info("counter: {} ", counter);
                }
            }

        }, "t2");

        t1.start();
        t2.start();
        t1.join();
        t2.join();

        log.info("最后counter: {} ", counter);


    }


}
